<?php
/**
 * 核心基类控制器
 */
namespace Core\Lib;
class Controller {
    /**
     * 请求连接Id
     * @var string 
     */
    public $connect_id = null;
    
    /**
     * 客户端请求对象
     * @var object 
     */
    public $request;
    
    /**
     * 客户端响应对象
     * @var object 
     */
    public $response;
    
    /**
     * GET 请求
     * @var array 
     */
    public $get = [];
    
    /**
     * POST 数据
     * @var array  
     */
    public $post = [];
    
    /**
     * 构造函数
     */
    public function __construct(){
        $this->initialize();
    }
    
    /**
     * 控制器初始化
     * @global type $_request
     * @global type $_response
     */
    public function initialize(){
        global $__request, $__response;
        $this->request = $__request;
        $this->response = $__response;
        if(property_exists($this->request, 'get')) $this->get = $this->request->get;
        if(property_exists($this->request, 'post')) $this->post = $this->request->post;
    }
    
    /**
     * Ajax 返回
     * @param array $data       返回数据
     * @return string
     */
    public function ajaxReturn($data=[], $type='json'){
        // 允许返回类型；json
        $allow_types = ['json'];
        if(!in_array($type, $allow_types)){
            return false;
        }
        
        // 返回类型处理函数
        $type_functions = [
            'json'  => 'json_encode'        // json 处理函数
        ];
        
        // 响应数据
        return call_user_func($type_functions[strtolower($type)], $data);
    }
}